//index.js
//获取应用实例
const app = getApp();
const mod = require('../../utils/util');
const articleUrl = require('../../config').article;
const prizeIndexUrl = require('../../config').prizeIndex;
const tagUrl = require('../../config').tag;
const drawUrl = require('../../config').draw;
const drawUserUrl = require('../../config').drawUser;

var formId;
var poilId = 0;

Page({
  data: {

    currentTab : 0,

    tag : [],

    list : [],

    formid : '',



    awardsList: {},
    animationData: {},
    btnDisabled: '',

    awardIndex: 0,

    poiltype: 0,

    chanceNum : 0,

    awardsConfig: {
      chance: true,
      awards: []
    },

    rid : null,

    crmshow : true,



    showTurntable : false
    
  },
  onLoad: function () {

    var that = this;


    



    // 绘制转盘
    var awardsConfig = this.data.awardsConfig.awards,
      len = awardsConfig.length,
      rotateDeg = 360 / len / 2 + 90,
      html = [],
      turnNum = 1 / len


    that.setData({
      btnDisabled: this.data.awardsConfig.chance ? '' : 'disabled'
    })


    for (var i = 0; i < len; i++) {
      // 奖项列表
      html.push({ turn: i * turnNum + 'turn', lineTurn: i * turnNum + turnNum / 2 + 'turn', award: awardsConfig[i].name });
    }

    that.setData({
      awardsList: html
    });




    app.getOpenid().then(function (res) {
      if (res.status == 200) {

        mod.postData(articleUrl, {
          openid: wx.getStorageSync('usercode').openid,
          sskey: wx.getStorageSync('usercode').sskey
        }, function (res) {
          if (res.data.code == 1) {
            that.setData({
              tag: res.data.data.tag,
              list: res.data.data.list
            })
          }
          else {
            mod.showToast(res.data.msg)
          }
        })



        mod.postData(prizeIndexUrl, {
          openid: wx.getStorageSync('usercode').openid,
          sskey: wx.getStorageSync('usercode').sskey
        }, function (res) {
          if (res.data.code == 1) {

            console.log(res.data.data)

   

            if(res.data.data.num >=1){

              that.setData({
                "awardsConfig.awards": res.data.data.plist
              })


              if(res.data.data.picture != "" && res.data.data.plist.length >0){
                that.setData({
                  showTurntable: true,
                  chanceNum: res.data.data.num
                })
              }
              else{
                that.setData({
                  showTurntable: false
                })
              }
            }
            else{
              that.setData({
                showTurntable: false
              })
            }
            
          }
          else {
            that.setData({
              showTurntable : false
            })
          }
        })







      } else {
        console.log(res.data);
      }
    }); 





  },
  go_detail : function(e){
    var _cid = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '../detail/detail?cid=' + _cid+''
    })
  },
  switchTab : function(e){
    var that = this;

    var tag = e.target.dataset.type

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }


    mod.postData(tagUrl, {
      openid : wx.getStorageSync('usercode').openid,
      sskey: wx.getStorageSync('usercode').sskey,
      tag: tag,
    }, function (res) {
      if (res.data.code == 1) {

        that.setData({
          list: res.data.data
        })


      } else {
        mod.showToast(res.data.msg)
      }

    })


    


  },
  formSubmit : function(e){
    formId = e.detail.formId;
    app.globalData.formid = formId;

    var _cid = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '../detail/detail?cid=' + _cid + ''
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '菲仕兰境界',
      path: 'pages/index/index'
    }
  },


  getLottery : function(){
    var that = this

    var changeNum = this.data.chanceNum;

    if (changeNum<=0){
      mod.showToast("没有抽奖次数咯")
      return
    }
    else{
      changeNum--
      that.setData({
        chanceNum: changeNum
      })
    }


    this.data.awardsConfig.chance = true;





    mod.postData(drawUrl, {
      openid: wx.getStorageSync('usercode').openid,
      sskey: wx.getStorageSync('usercode').sskey
    }, function (res) {

      console.log(res)

      //console.log(res.data.data)

      if (res.data.code == 1) {

        poilId = res.data.data.prize_id;

        that.setData({
          rid : res.data.data.rid
        })

        that.draw(poilId)

      }
      else if (res.data.code == -1) {

        that.draw(-1)
      }
      else if (res.data.code == -2) {
        mod.showToast("抽奖次数已用完")
      }
      else {
        mod.showToast("抽奖失败")
      }
    })





    //是否让抽
    


    






    
  },

  draw: function (poilId){

    var that = this;

    if (poilId == -1){
      for (var i = 0; i < that.data.awardsConfig.awards.length; i++) {
        if (that.data.awardsConfig.awards[i].type == 0) {
          that.setData({
            awardIndex: i
          })
        }
      }
    }
    else{
      for (var i = 0; i < that.data.awardsConfig.awards.length; i++) {
        if (that.data.awardsConfig.awards[i].id == poilId) {
          that.setData({
            awardIndex: i
          })
        }
      }
    }


    


    // 旋转抽奖
    that.runDegs = that.runDegs || 0
    console.log('deg', that.runDegs)
    that.runDegs = that.runDegs + (360 - that.runDegs % 360) + (360 * 8 - that.data.awardIndex * (360 / that.data.awardsConfig.awards.length))
    console.log('deg', that.runDegs)

    var animationRun = wx.createAnimation({
      duration: 4000,
      timingFunction: 'ease'
    })
    that.animationRun = animationRun
    animationRun.rotate(that.runDegs).step()
    that.setData({
      animationData: animationRun.export(),
      btnDisabled: 'disabled'
    })
  },

  animationend : function(){
    var that = this;

    if (this.data.awardsConfig.awards[this.data.awardIndex].type == 1) {
      setTimeout(function () {
        that.setData({
          poiltype: 1
        })
      }, 500)
    }
    else {
      wx.showModal({
        title: '很遗憾',
        content: '您没有获得奖品，加油哦！',
        showCancel: false
      })
    }


    if (this.data.awardsConfig.chance) {
      this.setData({
        btnDisabled: ''
      })
    }
  },

  submitdraw : function(e){
    console.log('form发生了submit事件，携带数据为：', e.detail.value);

    var that = this;


    if (e.detail.value.username == '' || e.detail.value.tel == '' || e.detail.value.address == ''){
      mod.showToast("请填写完整信息");
      return
    }


    mod.postData(drawUserUrl, {
      openid: wx.getStorageSync('usercode').openid,
      sskey: wx.getStorageSync('usercode').sskey,
      rid : that.data.rid,
      username: e.detail.value.username,
      tel: e.detail.value.tel,
      address: e.detail.value.address
    }, function (res) {
      if (res.data.code == 1) {
        mod.showToast("提交成功");
        setTimeout(function(){
          that.setData({
            poiltype: 0
          })
        },1000)
      }
      else {
        mod.showToast(res.data.msg)
      }
    })

  },
  closeturn : function(){
    this.setData({
      showTurntable : false
    })
  }

})
